package com.ocom.mqsimple.business.attendance;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.access.EntityAttendanceRules;
import com.ocom.common.entity.access.EntityMjGroupInfo;
import com.ocom.common.entity.access.EntityMjGroupPer;
import com.ocom.common.request.access.AddAttendanceReq;
import com.ocom.common.request.access.GetGroupPerReq;
import com.ocom.common.resp.Result;
import com.ocom.mqsimple.config.AttendanceConfig;
import com.ocom.mqsimple.feign.AccessClientService;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.MjrecodeClientService;
import com.ocom.redis.util.RedisUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Component
public class AttendanceConsumerMqListener {

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    AccessClientService accessClientService;

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    MjrecodeClientService mjrecodeClientService;

    private Logger logger = LoggerFactory.getLogger(AttendanceConsumerMqListener.class);

    @RabbitListener(queues = AttendanceConfig.Attendance_QUEUE)
    public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {

        String requestMessage = new String(message.getBody(), StandardCharsets.UTF_8);

        try {

            Object requestRedis = redisUtil.get(requestMessage);

            if (requestRedis == null) {
                logger.info("Attendance:redis为空异常 Suh7QZkc ");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                return;
            }

            EntityAttendanceRules entityAttendanceRules = JSON.parseObject(JSON.toJSONString(requestRedis), EntityAttendanceRules.class);

            GetGroupPerReq getGroupPerReq = new GetGroupPerReq();
            getGroupPerReq.setComId(entityAttendanceRules.getComId());
            getGroupPerReq.setSize(1);
            List<Integer> areaIds  = Arrays.asList(entityAttendanceRules.getAreaIds().split(",")).stream().map(x->Integer.parseInt(x)).collect(Collectors.toList());
            getGroupPerReq.setAreaIds(areaIds);

            Result<PageInfo<EntityMjGroupPer>> infoResult = accessClientService.getGroupPerList(getGroupPerReq);

            if (infoResult.getCode() != 200 || infoResult.getData().getTotal() == 0l) {
                logger.info("Attendance  Dtp6jv2X  无人员数据 【{}】【{}】", entityAttendanceRules, infoResult);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                return;
            }

            Result<List<EntityMjGroupInfo>> listResult = accessClientService.getGroupList(getGroupPerReq.getComId());

            getGroupPerReq.setSize(500);
            int listCount = (int)infoResult.getData().getTotal();
            int pageSize = 500;//设置每一个excel文件导出的数量   500的倍数  1次请求500条
            int quotient = listCount / pageSize + (listCount % pageSize > 0 ? 1 : 0);//循环次数

            AddAttendanceReq addAttendanceReq=JSON.parseObject(JSON.toJSONString(entityAttendanceRules),AddAttendanceReq.class);

            addAttendanceReq.setGroupList(listResult.getData());

            for (int i = 0; i < quotient; i++) {
                getGroupPerReq.setPage(i);
                infoResult = accessClientService.getGroupPerList(getGroupPerReq);
                if(infoResult.getData().getList().size()<=0)break;

                for (EntityMjGroupPer entityMjGroupPer : infoResult.getData().getList()) {
                    addAttendanceReq.setPId(entityMjGroupPer.getPId());
                    mjrecodeClientService.addAttendanceData(addAttendanceReq);
                }
            }


        } catch (Exception e) {
            logger.info("GzhMsgConsumerMqListener-出现异常队列消息开始进入死信队列{{}}", e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
